package com.grape.action.take_delivery;

import java.io.File;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.io.FileUtils;
import org.apache.struts2.ServletActionContext;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Namespace;
import org.apache.struts2.convention.annotation.ParentPackage;
import org.apache.struts2.convention.annotation.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Controller;

import com.grape.action.common.BaseAction;
import com.grape.domain.take_delivery.Promotion;
import com.grape.service.take_delivery.PromotionService;

@Namespace("/promotion")
@ParentPackage("json-default")
@Controller
@Scope("prototype")
public class PromotionAction extends BaseAction<Promotion> {
	private static final long serialVersionUID = 1L;

	@Action(value = "uploadImg", results = @Result(name = "success", type = "json"))
	public String uploadImg() throws IOException {

		// 设置绝对路径 第二条是从web.xml中获取路径
		String fileName = System.currentTimeMillis() + imgFileFileName;
		// String realFile =
		// ServletActionContext.getServletContext().getInitParameter("upload")+"/"+fileName;
		String realFile = ServletActionContext.getServletContext().getRealPath("/upload") + "/promotion/" + fileName;

		// 复制文件到此路径
		File fff = new File(realFile);
		FileUtils.copyFile(imgFile, fff);

		// 获取相对路径,完成上传
		String contextPath = ServletActionContext.getServletContext().getContextPath() + "/upload/promotion/"
				+ fileName;

		Map<String, Object> mp = new HashMap<>();
		mp.put("error", 0);
		mp.put("url", contextPath);

		ServletActionContext.getContext().getValueStack().push(mp);
		return SUCCESS;
	}

	
	// Error: java.lang.NumberFormatException: For input string: ""异常待处理
	@Action(value = "imgContent", results = @Result(name = "success", type = "json"))
	public String imgContent() {
		// 需要的文件格式
		String[] fileTypes = new String[] { "gif", "jpg", "jpeg", "png", "bmp" };

		// 相对与绝对路径		
		String realFile = ServletActionContext.getServletContext().getRealPath("/upload") + File.separator +"promotion"+ File.separator;
		System.out.println("绝对路径:"+realFile);
		String contextPath = ServletActionContext.getServletContext().getContextPath() + "/upload/promotion/";
		System.out.println("url路径:"+contextPath);

		// 使用绝对路径遍历
		File currentPathFile = new File(realFile);
		List<HashMap<String, Object>> fileList = new ArrayList<HashMap<String, Object>>();
		if (currentPathFile.listFiles() != null) {
			for (File file : currentPathFile.listFiles()) {
				HashMap<String, Object> hash = new HashMap<String, Object>();
				String fileName = file.getName();
				if (file.isDirectory()) {
					hash.put("is_dir", true);
					hash.put("has_file", (file.listFiles() != null));
					hash.put("filesize", 0L);
					hash.put("is_photo", false);
					hash.put("filetype", "");
				} else if (file.isFile()) {
					String fileExt = fileName.substring(fileName.lastIndexOf(".") + 1).toLowerCase();
					hash.put("is_dir", false);
					hash.put("has_file", false);
					hash.put("filesize", file.length());
					hash.put("is_photo", Arrays.<String>asList(fileTypes).contains(fileExt));
					hash.put("filetype", fileExt);
				}
				hash.put("filename", fileName);
				hash.put("datetime", new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(file.lastModified()));
				fileList.add(hash);
			}
		}

		// 将遍历结果返回页面
		HashMap<String, Object> result = new HashMap<String, Object>();
		result.put("moveup_dir_path", "");
		result.put("current_dir_path", realFile);
		result.put("current_url", contextPath);
		result.put("total_count", fileList.size());
		result.put("file_list", fileList);
		
		ServletActionContext.getContext().getValueStack().push(result);

		return SUCCESS;
	}

	@Action(value = "save", results = @Result(name = "success", location = "/pages/take_delivery/promotion.html", type = "redirect"))
	public String save() throws IOException {
		// 设置绝对路径 第二条是从web.xml中获取路径
		String fileName = System.currentTimeMillis() + titleImgFileFileName;
		// ServletActionContext.getServletContext().getInitParameter("upload")+"/"+fileName;
		String realFile = ServletActionContext.getServletContext().getRealPath("/upload") + "/promotion/" + fileName;

		// 复制文件到此路径
		File fff = new File(realFile);
		FileUtils.copyFile(titleImgFile, fff);

		// 获取相对路径,完成上传
		String contextPath = ServletActionContext.getServletContext().getContextPath() + "/upload/promotion/"+ fileName;
		model.setTitleImg(contextPath);
		model.setStatus("1");
		ps.save(model);
		
		return SUCCESS;
	}
	
	
	@Action(value="findLeaf",results=@Result(name="success",type="json"))
	public String findLeaf() {
		Pageable pageable = new PageRequest(page-1, rows);
		
		Page<Promotion> page = ps.findAll(pageable);
		
		ServletActionContext.getContext().getValueStack().push(page);
		return SUCCESS;
	}
	
	
	
	
	
	@Autowired
	private PromotionService ps;

	// 正儿八经的宣传标题图片
	private File titleImgFile;
	private String titleImgFileFileName;

	// 从文本域中来的图片存在这
	private File imgFile;

	private String imgFileFileName;
	private String imgFileContentType;

	public void setImgFile(File imgFile) {
		this.imgFile = imgFile;
	}

	public void setImgFileFileName(String imgFileFileName) {
		this.imgFileFileName = imgFileFileName;
	}

	public void setImgFileContentType(String imgFileContentType) {
		this.imgFileContentType = imgFileContentType;
	}

	public void setTitleImgFile(File titleImgFile) {
		this.titleImgFile = titleImgFile;
	}

	public void setTitleImgFileFileName(String titleImgFileFileName) {
		this.titleImgFileFileName = titleImgFileFileName;
	}
}
